package com.king.print;

import java.util.Map;

public class Printer {

	private static String printEmptyOrNull(String s) {
		if (s == null) return "[null]";
		if ("".equals(s)) return "[empty]";
		if (" ".equals(s)) return "[space]";
		if ("".equals(s.trim())) return "[blank]";
		return s;
	}

	public static <T> void print(T t) {
		System.out.println(printEmptyOrNull(String.valueOf(t)));
		System.out.println();
	}

	public static <T> void print(Iterable<T> iter) {
		int i = 0;

		for (T t : iter) {
			String s = String.valueOf(t);
			System.out.println("the " + (++i) + " element is: " + Printer.printEmptyOrNull(s));
		}
		System.out.println();
	}

	public static <K, V> void print(Map<K, V> map) {
		for (K k : map.keySet()) {
			V v = map.get(k);
			System.out.println("key " + (String.valueOf(k)) + " value is: " + Printer.printEmptyOrNull(String.valueOf(v)));
		}
		System.out.println();
	}

	public static <T> void print(T[] arr) {
		int i = 0;

		for (T t : arr) {
			String s = String.valueOf(t);
			System.out.println("the " + (++i) + " element is: " + Printer.printEmptyOrNull(s));
		}
		System.out.println();
	}

	public static void printTimeElapsed(Long start) {
		System.out.println("time elasped:[" + (System.currentTimeMillis() - start) + "] ms");
		System.out.println();
	}

	public static void printTimeElapsed(String event, Long start) {
		System.out.println("time elasped:[" + (System.currentTimeMillis() - start) + "] ms for [" + event + "]");
		System.out.println();
	}

	public static void print(String str, Exception ex) {
		System.out.println(str);
		System.out.println(ex);
	}
}
